<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :cols="1" :labelWidth="110" formType="add" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" :loading="addBtnState" @click="saveDialog">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import { addRole } from '@/api/common/auth/roleManage';
import { getFjxmmcList, getSfqfdmList } from '@/api/repair/business/customer/additional';
import * as types from '@/api/common/auth/roleManage/types';

export default {
  name: 'AdditionalProaddform',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      index: null,
      formList: this.createFormList()
    };
  },
  computed: {
    ...mapState('app', ['btnLoading']),
    addBtnState() {
      return this.btnLoading[types.YAN_ROLE_ADD];
    }
  },
  created() {
    if (this.type === 'edit') {
      this.formList.forEach(x => {
        x.initialValue = this.rowData[x.fieldName];
      });
    }
  },
  mounted() {
    this.getFjxmmcList();
    this.getSfqfdmList();
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'SELECT',
          itemList: [],
          label: '附加项目名称',
          fieldName: 'addItemName',
          placeholder: '请选择',
          rules: [{ required: true, message: '请输入附加项目名称', trigger: 'blur' }],
          change: val => {
            this.changeNameSelect(val);
          }
        },
        {
          type: 'INPUT',
          label: '附加项目代码',
          fieldName: 'addItemCode',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '附加项目费',
          fieldName: 'addItemPrice',
          disabled: true
        },
        {
          type: 'SELECT',
          itemList: [],
          label: '收费区分代码',
          fieldName: 'chargePartitionCode',
          placeholder: '请选择'
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'REMARK'
        }
      ];
    },
    openModel(data) {
      this.index = data.$index;
      // for (const item of this.formList) {
      //   item.initialValue = data[item.fieldName]
      // }
      this.formList[0].initialValue = data.ADD_ITEM_NAME;
      this.formList[1].initialValue = data.ADD_ITEM_CODE;
      this.formList[2].initialValue = data.ADD_ITEM_PRICE;
      this.formList[3].initialValue = data.CHARGE_PARTITION_CODE;
      this.formList[4].initialValue = data.REMARK;
      // console.log(this.formList)
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async getFjxmmcList() {
      const res = await getFjxmmcList();
      // console.log(res)
      this.formList[0].itemList = res.map(x => ({ value: x.ADD_ITEM_NAME, text: x.ADD_ITEM_NAME, code: x.ADD_ITEM_CODE, price: x.ADD_ITEM_PRICE }));
    },
    async getSfqfdmList() {
      const res = await getSfqfdmList();
      // console.log(res)
      this.formList[3].itemList = res.map(x => ({ value: x.CHARGE_PARTITION_CODE, text: x.CHARGE_PARTITION_NAME }));
    },
    // async saveDialog() {
    //   this.$refs.formPanel.SUBMIT_FORM();

    //   this.$set(this.saveData, 'dataType', '10461001')
    //   this.$set(this.saveData, 'dataSource', '10451001')
    //   if (this.saveData !== null) {
    //     const res = await addRole(this.saveData);
    //     if (res.resultCode === 200) {
    //       this.$notify.success({title: '提示信息',message:'新增成功!'});
    //       // 重新加载
    //       this.closeDialog('refresh');
    //     }else {
    //       this.$notify.error({title: '提示信息',message:res.errMsg||'新增失败!'});
    //       this.closeDialog()
    //     }
    //   }
    // },
    saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.formList[0].initialValue) {
        var data = {
          index: this.index,
          list: {}
        };
        data.list.ADD_ITEM_NAME = this.formList[0].initialValue;
        data.list.ADD_ITEM_CODE = this.formList[1].initialValue;
        data.list.ADD_ITEM_PRICE = this.formList[2].initialValue;
        data.list.CHARGE_PARTITION_CODE = this.formList[3].initialValue;
        data.list.REMARK = this.formList[4].initialValue;
        this.$emit('close', false, data);
      }
    },
    closeDialog() {
      this.$emit('close', false);
    },
    changeNameSelect(val) {
      // console.log(this.formList)
      for (const item of this.formList[0].itemList) {
        if (item.text === val) {
          this.formList[1].initialValue = item.code;
          this.formList[2].initialValue = item.price;
        }
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
